Chat Functions
Use the Chat adapter to manage chat rooms.
Importing the adapter
import { chatAdapter } from 'epicenter-libs';
The chatAdapter namespace exports functions that make calls to the Chat API.
For descriptions of the objects used by the Chat adapter functions, read Chat entities.
Create
Create chat
Creates a new chat with the provided name, scope, and permit.
The chat name provided in the room argument must be unique for the scope.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The create() function:
- Constructs a
POSTrequest to the endpoint/chat. - Accepts optional routing options to customize the API call.
- Returns the newly created
Chatobject.
export async function create(
room: string,
scope: GenericScope,
permit: Permit,
optionals: RoutingOptions = {}
): Promise<Chat>
Parameters
room(string): Name of the chat. Must be unique for the scope.scope(GenericScope): Scope of the chat. Must not be user specific.permit(Permit): A permit for the chat.optionals(RoutingOptions, optional): Network call option overrides.
Return value
A promise that resolves to the newly created Chat object.
Usage example
import { chatAdapter, SCOPE_BOUNDARY, ROLE } from 'epicenter-libs';
await chatAdapter.create(
'my-chat-room',
{ scopeBoundary: SCOPE_BOUNDARY.GROUP, scopeKey: '00000165ad4e6a3cd22b993340b569486321' },
{ readLock: ROLE.PARTICIPANT, writeLock: ROLE.PARTICIPANT }
);
Retrieve
Get chat
Retrieves a chat by its unique key.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The get() function:
- Constructs a
GETrequest to the endpoint/chat/{chatKey}. - Accepts optional routing options to customize the network call.
- Returns the corresponding
Chatobject.
export async function get(
chatKey: string,
optionals: RoutingOptions = {}
): Promise<Chat>
Parameters
chatKey(string): The unique key identifying the chat.optionals(RoutingOptions, optional): Network call option overrides.
Return value
A promise that resolves to the corresponding Chat object.
Usage example
import { chatAdapter } from 'epicenter-libs';
await chatAdapter.get('00000165ad4e6a3cd22b993340b658423159');
Query for chats
Searches for chats based on the provided search criteria, returning a paginated list.
For reference on search filters, read Filter and sort syntax.
Permissions
Requires a role of ANONYMOUS or higher.
Function description
The query() function:
- Constructs a
GETrequest to the endpoint/chat/searchwith the search criteria provided insearchOptions. - Accepts optional routing options to customize the API call.
- Returns a paginated list of
Chatobjects.
export async function query(
searchOptions: GenericSearchOptions,
optionals: RoutingOptions = {}
): Promise<Page<Chat>>
Parameters
searchOptions(GenericSearchOptions): Options for filtering and sorting the search.filter(string[], optional): Array of filter conditions. Multiple conditions can be combined.sort(string[], optional): Array of sorting criteria.first(number, optional): The index of the first item to return (default is 0).max(number, optional): The maximum number of items per page.
optionals(RoutingOptions, optional): Network call options overrides.
Return value
A promise that resolves to a Page<Chat> containing the list of chats found.
Usage example
import { chatAdapter } from 'epicenter-libs';
await chatAdapter.query({
filter: [
'room|=my-chat-room|my-other-chat|room-three',
'scopeBoundary=GROUP',
'created>=2022-01-03T20:30:53.054Z',
],
sort: ['+chat.created'],
first: 3,
max: 10,
});
Update
The only property of a chat that can be updated is its permit, which controls who has read and/or write permissions to the chat.
Manage chat access
To update the permit for a chat, call the updatePermit() function.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The updatePermit() function:
- Accepts optional routing options to customize the API call.
- Constructs a
PATCHrequest to the endpoint/chat/{chatKey}. - Sends a new
Permitobject in the request body. - Returns the updated
Chatobject.
export async function updatePermit(
chatKey: string,
permit: Permit,
optionals: RoutingOptions = {}
): Promise<Chat>
Parameters
chatKey(string): The unique key identifying the chat.permit(Permit): The new permit.optionals(RoutingOptions, optional): Network call options overrides.
Return value
A promise that resolves to the updated Chat object.
Usage example
import { chatAdapter } from 'epicenter-libs';
await chatAdapter.updatePermit(
'0000018d61f1217b22ce0ae605ff00659e3u',
{ readLock: ROLE.PARTICIPANT, writeLock: ROLE.PARTICIPANT }
);
Chat messages
Send message
Sends a message to a chat.
The sendMessage() function offers two options:
- Send a message to all users in the chat room.
- Send a private message to one of the users in the chat room.
To send the message only to a specific user, pass the optional userKey parameter.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The sendMessage() function:
- Constructs a
PUTrequest to the endpoint/chat/message/{chatKey}. - Optionally accepts a
userKeyto send the message only to a specific user within the chat. If omitted, the message is visible to everyone in the chat room. - Sends the provided
messagein the request body. - Returns the created
ChatMessageobject.
export async function sendMessage(
chatKey: string,
message: string,
optionals: { userKey?: string } & RoutingOptions = {}
): Promise<ChatMessage>
Parameters
chatKey(string): The unique key identifying the chat to send the message to.message(string): The text of the message to send.optionals(RoutingOptions, optional): Optional arguments, including network call option overrides.userKey(string, optional): The key of the user to send the message to. If omitted, the message is visible to everyone in the chat room.
Return value
A promise that resolves to the created ChatMessage object.
Usage example
import { chatAdapter } from 'epicenter-libs';
// Send a public message to a chat
await chatAdapter.sendMessage('0000017dd3bf540e5ada5b1e058f08f20461', 'hello');
// Send a private message to a specific user within the chat
await chatAdapter.sendMessage(
'0000017dd3bf540e5ada5b1e058f08f20461',
'hello, privately',
{ userKey: '000001796733eef0842f4d6d960997018a33' }
);
Get messages
Retrieves messages from a chat.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The getMessages() function:
- Accepts optional routing options to customize the API call.
- Constructs a
GETrequest to the endpoint/chat/message/{chatKey}. - Supports optional pagination parameters such as
maxRecordsandhorizon. For details, read the parameter descriptions below. - Returns a list of
ChatMessageobjects.
export async function getMessages(
chatKey: string,
optionals: {
maxRecords?: number,
horizon?: number,
} & RoutingOptions = {}
): Promise<ChatMessage[]>
Parameters
chatKey(string): The unique key identifying the chat.optionals({ maxRecords?: number; horizon?: number; } & RoutingOptions, optional): Optional arguments, including network call option overrides.maxRecords(number, optional): Maximum number of messages to retrieve.horizon(number, optional): The message ID to start from. Messages are retrieved in reverse order from this ID.
Return value
A promise that resolves to a list of ChatMessage objects.
Usage example
import { chatAdapter } from 'epicenter-libs';
// Get the chat message with id: 5
await chatAdapter.getMessages(
'0000017dd3bf540e5ada5b1e058f08f20461',
{ horizon: 5, maxRecords: 1 }
);
// Get the 10 chat messages starting from id 5 (inclusive)
await chatAdapter.getMessages(
'0000017dd3bf540e5ada5b1e058f08f20461',
{ horizon: 5, maxRecords: 10 }
);